//
//  TJEmotionPackage.swift
//  表情键盘
//
//  Created by 唐后能 on 16/4/21.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class TJEmotionPackage: NSObject {

    // id表情包的文件夹名称.
    var id:String
    
    // 表情包名称.
    var group_name_cn:String
    
    // 表情模型数组
    var emoticons:[TJEmotion]
    
    // 分页表情模型数组.
    var pageEmotions:[[TJEmotion]] = [[TJEmotion]]()
    
    // 字典转模型.
    init(id:String,group_name_cn:String,emoticons:[TJEmotion]) {
        self.id = id
        self.group_name_cn = group_name_cn
        self.emoticons = emoticons
        super.init()
        setValueForPageEmotions ()
    }
    
    // 给模型中没有的key赋空值
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    // 给分页表情数组赋值.-->将模型截取成一页一页的.
    private func setValueForPageEmotions (){
    
        // 计算页数-->和通过总数和列数计算行数一样.
        // 页数 = (表情总个数 + 每页的个数 - 1) / 每页的个数
        let pageCount = (emoticons.count + numberOfPage - 1) / numberOfPage
        
        // 当没有表情的时候,生成一个空的表情页.
        if pageCount == 0 {
        
            let pageEmotion = [TJEmotion]()
            pageEmotions.append(pageEmotion)
        }
        
        // 获取每一页在表情包中的range.
        for i in 0..<pageCount { [[0]]
        
            let location = numberOfPage * i
            var length = numberOfPage
            
            // 最后一组,给length重新赋值.
            if location+length > emoticons.count {
            
                length = emoticons.count - location
            }
            
            let range = NSRange(location: location, length: length)
            let pageEmotion = (emoticons as NSArray).subarrayWithRange(range) as! [TJEmotion]
            pageEmotions.append(pageEmotion)
        }
    }
    
    // 打印对象.
    override var description:String {
        
        get {
            
            let keys = ["id","group_name_cn","emoticons","pageEmotions"]
            return "EmotionPackage模型:\(dictionaryWithValuesForKeys(keys).description)"
        }
    }

}
